<template>
  <div class="swiper-container" :ref="someRef">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="banner in someList" :key="banner.id">
        <img :src="banner.imgUrl" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from "swiper"
import "swiper/css/swiper.min.css"
export default {
  // 1.监视的数据需要是父组件传递过来的someList
  // 2.模板渲染时 渲染父组件传递过来的someList
  // 3. ref需要时传递过来的someRef
  // 4. new Swiper传递的真实dom节点 也得是父组件传递过来得someRef
  name: "Carousel",
  props: ["someList", "someRef"],
  watch: {
    someList: {
      handler(newVal, oldVal) {
        this.$nextTick(() => {
          new Swiper(this.$refs[this.someRef], {
            spaceBetween: 30, //控制每一屏中间的间隔
            speed: 1000, //切换时间
            loop: true, // 无缝轮播
            autoplay: {
              //自动轮播
              delay: 1000, //延时时间 当前屏显示多久 去下一张
              disableOnInteraction: false, //鼠标操作之后 是否禁止自动轮播
            },
            pagination: {
              //小圆点
              el: ".swiper-pagination", //选择到那个小圆点元素
              clickable: true, //是否可以点击小圆点
            },
            navigation: {
              //左右按钮
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          })
        })
      },

      immediate: true,
    },
  },
}
</script>

<style lang="less" scoped></style>
